import Vue from 'vue'
import VueRouter from 'vue-router'
import Reg from '@/views/Reg/Reg.vue'
import Login from '@/views/Login/Login.vue'
import Main from '@/views/Main/Main.vue'
import store from '@/store'
import Home from '@/views/menus/Home/Home.vue'
import UserInfo from '@/views/menus/User/UserInfo.vue'
import UserAvatar from '@/views/menus/User/UserAvatar.vue'
import UserPwd from '@/views/menus/User/UserPwd.vue'
import ArtCate from '@/views/menus/Article/ArtCate.vue'
Vue.use(VueRouter)

const routes = [
  {
  path:'/',
  component:Main,
  redirect:'/home',
  children:[
    {
      path:'home',
      component:Home
    },{
      path:'user-info',
      component:UserInfo
    },
    {
      path:'user-avatar',
      component:UserAvatar
    },
    {
      path:'user-Pwd',
      component:UserPwd
    },
    {
      path:'art-cate',
      component:ArtCate
    },
    
  ]
  },
 {
   path:'/reg',
   component:Reg
 },
 {
   path:'/login',
   component:Login
 }
]



const router = new VueRouter({
  routes
})

router.beforeEach((to,from,next)=>{
  const token =store.state.token
  // if(!token&&to.path!=='/login'&&to.path!=='/reg'){
  //   next('/login')
  // }else{
  //   next()
  // }
  if (to.path === '/login' || to.path === '/reg') {
    next()
  } else {
    if (!token) {
      next('/login')
    } else {
      next()
    }
  }

})

export default router
